﻿using System;
using System.ComponentModel;

namespace Softlanding.ComponentModel
{
    /// <summary>
    /// Specifies a localized description for a property or event.
    /// </summary>
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class SLWebDescriptionAttribute : DescriptionAttribute
    {
        private readonly string _classKey;
        private readonly string _resourceKey;

        /// <summary>
        /// Initializes a new instance of the SLWebDescriptionAttribute class
        /// </summary>
        /// <param name="classKey">The base file name of the language resource file containing a localized description name</param>
        /// <param name="resourceKey">The name half of a name/value pair in a resource file</param>
        public SLWebDescriptionAttribute(string classKey, string resourceKey)
        {
            _classKey = classKey;
            _resourceKey = resourceKey;
        }

        /// <summary>
        /// Gets the localized description stored in this attribute
        /// </summary>
        public override string Description
        {
            get
            {
                var value = CommonHelper.GetResourceString(_classKey, _resourceKey);
                if (String.IsNullOrEmpty(value))
                {
                    value = _resourceKey;
                }
                return this.DescriptionValue = value;
            }
        }
    }
}